package CSC;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import com.google.gson.Gson;

public class ChatClient {
    private Socket socket;
    //每一个客户端拥有一个message对象，象征着身份信息
    private MessageBean message;
    //GSON是Google开发的Java API，用于转换Java对象和Json对象。
    private Gson gson = new Gson();
    private int number;
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public ChatClient(int num,int id) {
        message = new MessageBean();
        this.number = num;
        message.setFriendID(id);
        try {
            socket = new Socket(SocketUrls.IP, SocketUrls.PORT);
            intiMessage();
            System.out.println(message.getUserName()+"已连接服务器");
            //接收与转发数据各开一个线程，实现异步交互
            receiveMessage();
            sendMessage();  
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 初始化用户基本信息
    private void intiMessage() {

        message.setUserID(this.number);
        message.setGroupID(1);
        message.setUserName("admin"+this.number);
        message.setMessageID(2);
        message.setChatType(1);
        message.setFirst(true);
        message.setContent("&");
    }

    // 开个线程接收消息
    private void receiveMessage() {
        new Thread(new Runnable() {
            /* (non-Javadoc)
             * @see java.lang.Runnable#run()
             */
            /* (non-Javadoc)
             * @see java.lang.Runnable#run()
             */
            /* (non-Javadoc)
             * @see java.lang.Runnable#run()
             */
            public void run() {
                try {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    String line ;
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    line = in.readLine();
                    //System.out.println(line);
                    while (line != null) {
                        MessageBean messageBean = gson.fromJson(line, MessageBean.class);
                        System.out.println("用户 : " + messageBean.getUserName());
                        System.out.println("内容 : " + messageBean.getContent());
                        line = in.readLine();
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void sendMessage() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    PrintWriter write = new PrintWriter(socket.getOutputStream());

                    String GSON = gson.toJson(message);
                    write.println(GSON);
                    write.flush();
                    //System.out.println("请输入好友ID");
                    //message.setFriendID(Integer.parseInt(br.readLine()));
                    System.out.println("请输入内容");
                    String str = br.readLine();
                    message.setFirst(false);
                    //System.out.println(str);
                    while (str != null && !str.equals("end")) {
                        //System.out.println(str);
                        message.setContent(str);
                        //System.out.println(message.getContent());
                        GSON = gson.toJson(message);
                        //System.out.println(GSON);
                        write.println(GSON);
                        write.flush();
                        System.out.println("请输入内容");
                        str = br.readLine();
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void main(String[] args) {
        new ChatClient(1,2);
    }

}
